<?php
namespace app\h5api\controller;

// H5
use app\common\model\Banner;
use app\common\model\Comment;
use app\common\model\OrderDetail;
use app\common\model\Shops;
use app\common\model\Vcode;
use plug\SUtil;
use plug\SHttp;
use think\facade\Config;
use app\common\service\Yunpian;

class Main extends Base
{

    /**
     * 首页
     */
    public function index()
    {
        $bannerModel = new Banner();

        // 首页轮播图
        $bannercond['pos'] = 1;
        $banner1 = $bannerModel->get_list($bannercond,'id,pic_url,to_url','sort asc');


        // 家政轮播图
        $bannercond['pos'] = 2;
        $banner2 = $bannerModel->get_list($bannercond,'id,pic_url,to_url','sort asc');

        $tvar = [
            'banner1' => $banner1,
            'banner2' => $banner2,
        ];
        $this->result($tvar,'1');
    }

    /**
     * 新品轮播图
     */
    public function NewBanner()
    {
        $bannerModel = new Banner();

        // 新品轮播图
        $bannercond['pos'] = 3;
        $banner1 = $bannerModel->get_list($bannercond,'id,pic_url,to_url','sort asc');

        $this->result($banner1,'1');
    }



    /**
     * 商品列表
     */
    public function ProductList()
    {
        $productModel = new \app\common\model\Products();

        // 显示类型
        $show = input('show','');
        // 店铺ID
        $shop_id = input('shop_id',0);
        // 类型ID
        $type_id = input('type_id', 0);
        // 商品模糊查询
        $cond['product_name'] = input('name','');
        // 排序类型：综合，销量，价格
        $sort_type = input('sort_type', 4);
        // 排序： 正序，倒序
        $sort = input('sort', 1);
        // 是否要分页
        $is_page = input('is_page',1);


        $cond['show'] = $show;

        $page = input('page');
        $limit = input('limit');

        $cond['state'] = 1;
        $cond['approved'] = 2;
        if($shop_id > 0){
            $cond['shop_id'] = $shop_id;
        }
        if($type_id > 0){
            $cond['type_id'] = $type_id;
        }

        $orderby = "";
        // 排序类型
        if($sort_type == 1){
            // 综合
            $orderby = $orderby."createat";
        }elseif ($sort_type == 2){
            // 销量
            $orderby = $orderby."salecount";
        }elseif ($sort_type == 3){
            // 价格
            $orderby = $orderby."mprice";
        } elseif ($sort_type == 4){
			// 价格
			$orderby = $orderby."sort";
		}
        // 排序
        if($sort == 1){
            // 正序
            $orderby = $orderby." asc";
        }else{
            // 倒序
            $orderby = $orderby." desc";
        }

        $field = "product_id,product_name,price,mprice,imageurl,jifen,integral,express_fee";

        $rs_data = $productModel->get_list_resule($cond,$orderby,$field,$limit, $page, $is_page);

        $this->result($rs_data,1);
    }

    /**
     * 店铺信息
     */
    public function ShopInfo()
    {
        $shopModel = new Shops();
        $shopId = input('shop_id');

        $shop = $shopModel->getOne(['shop_id' => $shopId],'*');

        $this->result($shop, 1);
    }

    /**
     * 检查订单过期
     */
    public function CheckOrder()
    {
        $orderModel = new \app\common\model\Order();
        $detailModel = new OrderDetail();
        $commentModel = new Comment();

        $cond['pay_status'] = 1;
        $cond['order_status'] = 1;
        $rs_data = $orderModel->get_list($cond,'','order_id,order_time');

        foreach ($rs_data as $key => $val){
            // 半个小时的时间戳
            $time = calculate([time(), $val['order_time']],'-',0);

            if($time > 1800){
                $orderModel->where('order_id', $val['order_id'])->update(['order_status' => 5,'pay_status' => 3]);
            }
        }

        $where['order_status'] = 4;
        $where['pay_status'] = 2;
        $where['is_comment'] = 2;
        $where['order_type'] = 1;

        $rs = $orderModel->get_list($where,'','order_id,order_time,user_id');
        foreach ($rs as $key => $val){
            // 72小时时间戳
            $time = calculate([time(), $val['order_time']],'-',0);

            $times = calculate([86400, 3],'*',0);

            if($time > $times){
                $orderModel->where('order_id', $val['order_id'])->update(['is_comment' => 1]);
                $detail = $detailModel->get_list(['order_id' => $val['order_id']],'','detail_id,product_id,p_s_id,p_s_name,quantity');

                foreach ($detail as $key1 => $val1){
                    $stock_info = [
                        "p_s_id" => $val1['p_s_id'],
                        "p_s_name" => $val1['p_s_name'],
                        "stock" => $val1['quantity'],
                        "product_id" => $val1['product_id'],
                        "order_time" => $val['order_time'],
                    ];

                    $data = [
                        'content'   => '默认评价',
                        'star'      => '5',
                        'order_id'      => $val['order_id'],
                        'user_id'      => $val['user_id'],
                        'type'      => 1,
                        'product_service_id'      => $val1['product_id'],
                        'order_detail_id'      => $val1['detail_id'],
                        'is_show'      => 1,
                        'proterty_details' => json_encode($stock_info,true),
                    ];
                    $rs = $commentModel->edit($data);
                }
            }
        }

    }

	/*
	 * 发送验证码
	 */
	public function SendCode()
	{
		// 用户电话号码
		$user_name = input('phone');
		//用户两分钟内是否发送过验证码
		if(empty($user_name) || !is_numeric($user_name)){
			$this->result('',-1,'输入正确的手机号码');
		}
		$vcodeModel = new Vcode();
		if($vcodeModel->CheckVcode($user_name)){
			$this->result('',-1,'发送频繁，验证码两分钟内有效');
		}

		// 获取随机验证码
		$code = SUtil::random(6,1);

		$rs = $vcodeModel->edit(["phone" => $user_name, "msg" => $code]);
		if($rs === false){
			$this->result('',-1,'请重新发送');
		}

		// 调用发送短信接口
		$Yunpian = new Yunpian;
		$rs = $Yunpian->Sendvcode($user_name, $code);
		// json 解密
		$arr = json_decode($rs['str'],true);
		if($rs['status']==200){
			if($arr['code'] == 0){
				$this->result('',1,'发送成功');
			}
		}
		$this->result('',-1,$arr['msg']);

	}

    /**
     * 地图请求
     */
    public function MapRequest()
    {
        $lon = input('post.lon');
        $lat = input('post.lat');

        if(empty($lon) || empty($lat)){
            $this->result([],-1,'缺乏上传参数');
        }
        $mapconfig = Config::pull('mapconfig');
        $location = $lat.','.$lon;
        $url = "https://apis.map.qq.com/ws/geocoder/v1/?location={$location}&key={$mapconfig['Key']}&get_poi=0";
        $return = SHttp::http_curl($url);
        $data = json_decode($return['str'],true);
//        var_dump($data);die;
        if($data['status'] !== 0){
            $this->result([],-1,$data['message']);
        }
        $this->result($data['result'],1,'返回成功');
    }

	/*
	 * 测试
	 */
	public function testSendCode()
	{
		// 用户电话号码
		$user_name = input('phone');
		if(empty($user_name) || !is_numeric($user_name)){
			$this->result('',-1,'输入正确的手机号码');
		}

		// 调用发送短信接口
		$Yunpian = new Yunpian;
		$rs = $Yunpian->SendNoticeOrder($user_name);
//		$rs = $Yunpian->SendNoticeCancelOrder($user_name);
//		$rs = $Yunpian->SendNoticeservice($user_name);
//		$rs = $Yunpian->SendNoticeCancelservice($user_name);
		// json 解密
		$arr = json_decode($rs['str'],true);
		if($rs['status']==200){
			if($arr['code'] == 0){
				$this->result('',1,'发送成功');
			}
		}
		$this->result('',-1,$arr['msg']);
	}
}
